<script setup>
import { ref } from 'vue';
import router from '../../router'; // 引入路由配置
import axios from 'axios'
import { showNotify } from 'vant';
import { useMainStore } from "../../store";
const useStore = useMainStore();
const username = ref('');
const password = ref('');
const onSubmit = (values) => {
    console.log('submit', values);
    let data = {
      username: values.name,
      password: values.password
    }
    axios.post("/api/user/login", data).then(res => {
      console.log(res);
      if(res.data.code == 1){
        showNotify({ type: 'primary', message: res.data.msg });
      }else{
        useStore.token = res.data.msg
        router.push('/');
      }
    })
};

const register = () => {
    router.push("/register")
}
</script>

<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="name"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
        <van-button class="register" round block type="primary" @click="register">
          注册
    </van-button>
      </div>
    </van-form>
    
  </div>
</template>

<style scoped>
.register{
    margin-top: 10vw;
}
</style>
